<?php

use App\Models\UserBinds;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserBindsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_binds', function (Blueprint $table) {

            $table->increments('id')->comment('表ID');

            $table->unsignedInteger('user_id')->comment('用户ID');

            $table->enum('type', [
                UserBinds::TYPE_QQ,
                UserBinds::TYPE_WEIXIN,
                UserBinds::TYPE_TAOBAO,
                UserBinds::TYPE_WEIBO,
            ])->comment('账号类型');

            $table->string('open_id', 100)->comment('第三方用户ID');
            $table->string('access_token', 100)->nullable()->comment('ACCESS_TOKEN');
            $table->string('refresh_token', 100)->nullable()->comment('REFRESH_TOKEN');
            $table->timestamp('access_token_expires')->nullable()->comment('ACCESS_TOKEN过期时间');
            $table->timestamp('refresh_token_expires')->nullable()->comment('REFRESH_TOKEN过期时间');

            $table->unique('open_id');

            // 外键,删除用户时删除该用户的所有已绑定账户记录
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');

            $table->comment = '用户账号绑定表';
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_binds');
    }
}
